import constantRoutes from '@/router/routes'
import { getMenus } from '@/api/permission'

const state = {
  permissionList: [],
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_PERMISSIONLIST: (state, permissionList) => {
    state.permissionList = permissionList
  },
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  getPermissionList({ commit }) {
    return new Promise((resolve, reject) => {
      let params = {}
      getMenus(params).then(response => {
          commit('SET_PERMISSIONLIST', response.data)
          resolve(response)
      }).catch(error => {
          reject(error)
      })
    })
  },
  updateAppRouter({ commit }, routes) {
    return new Promise(resolve => {
      let routelist = routes.constRoutes
      commit('SET_ROUTES', routelist)
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
